package metrics

import (
	"gitee.com/captials-team/ubdframe/src/common"
	"gitee.com/captials-team/ubdframe/src/domain/configstc"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"net/http"
)

// NativePrometheusHelper 原生prometheus辅助器，pull模式
type NativePrometheusHelper struct {
	Registry *prometheus.Registry

	conf configstc.MonitorReportConfig
}

// NewPrometheusHelper 指标上报器-prometheus pull模式
func NewPrometheusHelper(conf configstc.MonitorReportConfig) *NativePrometheusHelper {
	st := &NativePrometheusHelper{
		conf:     conf,
		Registry: prometheus.NewRegistry(),
	}
	return st
}

func (h *NativePrometheusHelper) RunHttp() {
	http.Handle("/metrics", promhttp.HandlerFor(
		h.Registry,
		promhttp.HandlerOpts{},
	))
	common.ErrPanic(http.ListenAndServe(h.conf.Host, nil))
}
